﻿using JncInterProcess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Drawing.Imaging;
using HysVision;
using UIDATALayer;
namespace VisionBaseLib
{
    public enum VisionType
    {
        高纳视觉,
        HY视觉,
    }
    public class HYVisionData : ShowData
    {
        public Bitmap 原始图像 { get; set; }
        public Bitmap 图形图像 { get; set; }
        public string 相机名称 { get; set; }
        public string 流程名称 { get; set; }
    }
    public class GaoNaVisionData : ShowData
    {
        public int n_Index { get; set; }
        public Bitmap img { get; set; }
    }
    public class DefineVisionData : ShowData
    {
        public Bitmap 原始图像 { get; set; }
        public Bitmap 图形图像 { get; set; }
        public string 相机名称 { get; set; }
    }

    public class VisionBaseMgr
    {
        private static VisionBaseMgr visionBaseMgr = null;
        private static object obj = new object();
        public VisionBaseMgr()
        {

        }
        public static VisionBaseMgr GetInstance()
        {
            if (visionBaseMgr == null)
            {
                lock (obj)
                {
                    visionBaseMgr = new VisionBaseMgr();
                    return visionBaseMgr;
                }

            }
            else
                return visionBaseMgr;
        }
        public delegate void EventShowImage(string stationName, Bitmap image_Ori,Bitmap image_Draw);
        public EventShowImage OnTest_ShowImage = null;
        public GNVision _gNVisionDisp = new GNVision();
        public HYVision hyVision = new HYVision();
        public Dictionary<object, Image> dic_Image = new Dictionary<object, Image>();
        public Dictionary<object, Image> dic_ImageDraw = new Dictionary<object, Image>();
        VisionType type = VisionType.HY视觉;
        public void Init(VisionType visionType)
        {
            type = visionType;
            if (visionType==VisionType.高纳视觉)
            {
                _gNVisionDisp.Init();
                _gNVisionDisp.StartRun();
            }
            else
            {
                hyVision.Init();
            }
        }
        public void Function_Hide()
        {
            if (type == VisionType.高纳视觉)
            {
                _gNVisionDisp.HideVisionWindow();
            }
            else
            {
                hyVision.HideVisionWindow();
            }
        }
        public void Function_切换机种(string strName)
        {
            if (type == VisionType.高纳视觉)
            {
                _gNVisionDisp.SwitchProgram(strName);
            }
            else
            {
                hyVision.SwitchProgram(strName);
            }
        }
        public object o_ShowVisionImage = new object();
        public void Function_SetImage(object obj_Index,Image image,Image imageDraw)
        {
            if (!dic_Image.ContainsKey(obj_Index))
            {
                dic_Image.Add(obj_Index,(Bitmap) image.Clone());
                dic_ImageDraw.Add(obj_Index, (Bitmap)imageDraw.Clone());
            }
            else
            {
                dic_Image[obj_Index].Dispose();
                dic_Image[obj_Index] = (Bitmap)image.Clone();
                dic_ImageDraw[obj_Index].Dispose();
                dic_ImageDraw[obj_Index] = (Bitmap)imageDraw.Clone();
            }
        }

        public void Function_SaveImage(object obj_Index, string str_机型,string str_批次, bool b_OK, string strSn, string strType)
        {
            if (!dic_Image.ContainsKey(obj_Index))
                return;
            if (dic_Image[obj_Index] == null)
                return;
            string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
            strSn = r.Replace(strSn, "");
            string strImagePath = $"D:\\HY System\\图片\\{str_机型}\\{str_批次}\\" +
                       $"{DateTime.Now.ToString("yyyy-MM-dd")}";
            string strImagePathOri = "";
            string strImagePathTest = "";
            if (strType.Contains("白场"))
            {
                 strImagePathOri = b_OK ? strImagePath + $"\\白场OK\\{strSn}\\{strSn}_{strType}_原图_{DateTime.Now.ToString("HH_mm_ss")}.png" : strImagePath + $"\\白场NG\\{strSn}\\{strSn}_{strType}_原图_{DateTime.Now.ToString("HH_mm_ss")}.png";
                 strImagePathTest = b_OK ? strImagePath + $"\\白场OK\\{strSn}\\{strSn}_{strType}_测试图_{DateTime.Now.ToString("HH_mm_ss")}.png" : strImagePath + $"\\白场NG\\{strSn}\\{strSn}_{strType}_测试图_{DateTime.Now.ToString("HH_mm_ss")}.png";
            }
            else
            {
                 strImagePathOri = b_OK ? strImagePath + $"\\胶型检测OK\\{strSn}\\{strSn}_{strType}_原图_{DateTime.Now.ToString("HH_mm_ss")}.png" : strImagePath + $"\\胶型检测NG\\{strSn}\\{strSn}_{strType}_原图_{DateTime.Now.ToString("HH_mm_ss")}.png";
                 strImagePathTest = b_OK ? strImagePath + $"\\胶型检测OK\\{strSn}\\{strSn}_{strType}_测试图_{DateTime.Now.ToString("HH_mm_ss")}.png" : strImagePath + $"\\胶型检测NG\\{strSn}\\{strSn}_{strType}_测试图_{DateTime.Now.ToString("HH_mm_ss")}.png";
            }
            string strPATH = Directory.GetParent(strImagePath).FullName;
            if (!Directory.Exists(strImagePathOri))
            {
                Directory.CreateDirectory(Directory.GetParent(strImagePathOri).FullName);
            }
            if (!Directory.Exists(strImagePathTest))
            {
                Directory.CreateDirectory(Directory.GetParent(strImagePathTest).FullName);
            }
            Task.Run(() =>
            {
                try
                {
                    lock (o_ShowVisionImage)
                    {
                        dic_Image[obj_Index].Save(strImagePathOri, ImageFormat.Png);
                        dic_ImageDraw[obj_Index].Save(strImagePathTest, ImageFormat.Png);
                    }
                }
                catch (Exception)
                {

                }
            });

        }
    }
}